package com.zhaopin.thrift;

import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.apache.thrift.transport.layered.TFramedTransport;
import thrift.generated.PersonService;

/**
 * // 物理层 数据层 网络层 传输层 会话层 表示层 应用层
 * @author yin.peide
 * @date 2021-12-09 16:09
 *
 */

public class ThriftServer {

    public static void main(String[] args) throws TTransportException {

        TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);
        //半同步半异步的一个 server
        THsHaServer.Args arg = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);
        PersonService.Processor<PersonServiceImpl> processor = new PersonService.Processor<>(new PersonServiceImpl());

        // TCompactProtocol 协议层所使用的对象，这个是一个二进制压缩协议
        arg.protocolFactory(new TCompactProtocol.Factory());

        //TFramedTransport 表示为传输层所使用的对象
        arg.transportFactory(new TFramedTransport.Factory());
        arg.processorFactory(new TProcessorFactory(processor));

        TServer server = new THsHaServer(arg);
        System.out.println("Thrift Server Started!");

        //持续提供服务
        server.serve();

    }
}
